#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using System;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace pigs
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class Menu : MenuScreen
    {
        ContentManager content;
        GameSettings settings;

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public Menu(GameSettings set)
            : base("When Pigs Fly")
        {
            this.settings = set;

            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry("Story Mode");
            MenuEntry survivalGameMenuEntry = new MenuEntry("Survival Mode");
            MenuEntry controlsMenuEntry = new MenuEntry("Controls");
            MenuEntry optionsMenuEntry = new MenuEntry("Options");
            MenuEntry scoresMenuEntry = new MenuEntry("High Scores");
            MenuEntry achievMenuEntry = new MenuEntry("Achievements");
            MenuEntry beastsMenuEntry = new MenuEntry("Beastiary");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            survivalGameMenuEntry.Selected += SurvivalGameMenuEntrySelected;
            controlsMenuEntry.Selected += ControlsMenuEntrySelected;
            optionsMenuEntry.Selected += OptionsMenuEntrySelected;
            scoresMenuEntry.Selected += ScoresMenuEntrySelected;
            achievMenuEntry.Selected += achievMenuEntrySelected;
            beastsMenuEntry.Selected += beastsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(survivalGameMenuEntry);
            MenuEntries.Add(controlsMenuEntry);
            MenuEntries.Add(optionsMenuEntry);
            MenuEntries.Add(scoresMenuEntry);
            MenuEntries.Add(achievMenuEntry);
            MenuEntries.Add(beastsMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        
            GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
            GamePad.SetVibration(PlayerIndex.Two, 0f, 0f);
            GamePad.SetVibration(PlayerIndex.Three, 0f, 0f);
            GamePad.SetVibration(PlayerIndex.Four, 0f, 0f);

        }

        public override void LoadContent()
        {
            base.LoadContent();
            if (settings.startingUp)
            {
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services, "Content");

                SoundEffect music = content.Load<SoundEffect>("sounds/backgroundsmall");
                this.settings.musicInstance = music.CreateInstance();
                this.settings.musicInstance.IsLooped = true;
                if (settings.playMusic)
                {
                    this.settings.musicInstance.Play();
                }
                settings.achievements.bcAim.image = content.Load<Texture2D>("achievements/mountainsaccuracy");
                settings.achievements.skAim.image = content.Load<Texture2D>("achievements/prairieaccuracy");
                settings.achievements.cityAim.image = content.Load<Texture2D>("achievements/cityaccuracy");
                settings.achievements.bcNoEscape.image = content.Load<Texture2D>("achievements/mountainsnoescapes");
                settings.achievements.skNoEscape.image = content.Load<Texture2D>("achievements/prairienoescapes");
                settings.achievements.cityNoEscape.image = content.Load<Texture2D>("achievements/citynoescapes");
                settings.achievements.bcNoHits.image = content.Load<Texture2D>("achievements/mountainnohits");
                settings.achievements.skNoHits.image = content.Load<Texture2D>("achievements/prairienohits");
                settings.achievements.cityNoHits.image = content.Load<Texture2D>("achievements/citynohits");
                settings.achievements.sur25.image = content.Load<Texture2D>("achievements/wave25");
                settings.achievements.sur50.image = content.Load<Texture2D>("achievements/wave50");
                settings.achievements.sur100.image = content.Load<Texture2D>("achievements/wave100");

                settings.startingUp = false;
            }
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new MapLevelMenu(ScreenManager, settings), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void ScoresMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new HighScoreScreen(ScreenManager, settings.hs), e.PlayerIndex);

        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void SurvivalGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
               new Survival(settings));

            // You can add the screen immediately with this line and then comment
            // the line above out, but there is some "lag" due to load time
            //ScreenManager.AddScreen(new GameplayScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Controls menu entry is selected.
        /// </summary>
        void ControlsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new ControlsMenuScreen(ScreenManager, false), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Controls menu entry is selected.
        /// </summary>
        void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new OptionsMenuScreen(ScreenManager, settings, true), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Controls menu entry is selected.
        /// </summary>
        void achievMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new AchievScreen(ScreenManager, settings.achievements), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Controls menu entry is selected.
        /// </summary>
        void beastsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new BeastsScreen(ScreenManager, false), e.PlayerIndex);
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
        }
    }
}
